//
//  AddrBar.m
//  XPhoto
//
//  Created by baboy on 2019/6/28.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "AddrBar.h"
#define VIEW_MARGIN 10
#define BUTTON_HEIGHT 16
@implementation AddrBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGRect butttonFrame = CGRectZero;
    if (self.serialButton && !self.serialButton.hidden) {
        
        CGSize textSize = [self.post.serial.title sizeWithAttributes:@{NSFontAttributeName: self.serialButton.titleLabel.font}];
        butttonFrame.size = CGSizeMake(textSize.width+10, BUTTON_HEIGHT);
        self.serialButton.frame = butttonFrame;
        butttonFrame.origin.x += butttonFrame.size.width+5;
    }
    if (self.addrButton && !self.addrButton.hidden){
        CGSize textSize = [self.post.addr sizeWithAttributes:@{NSFontAttributeName: self.addrButton.titleLabel.font}];
        butttonFrame.size = CGSizeMake(textSize.width+10, BUTTON_HEIGHT);
        self.addrButton.frame = butttonFrame;
    }
}
- (void)setPost:(Post *)post{
    _post = post;
    [self createViews];
}
- (void)createViews{
    if ( !self.post || !self.post.serial ) {
        self.serialButton.hidden = YES;
    }
    if ( !self.post || !self.post.addr){
        self.addrButton.hidden = YES;
    }
    
    CGRect butttonFrame = CGRectMake(VIEW_MARGIN, 0, 0, 0);
    if ( self.post.serial) {
        self.serialButton.hidden = NO;
        CGSize textSize = [self.post.serial.title sizeWithAttributes:@{NSFontAttributeName: self.serialButton.titleLabel.font}];
        butttonFrame.size = CGSizeMake(textSize.width+20, BUTTON_HEIGHT);
        self.serialButton.frame = butttonFrame;
        self.serialButton.layer.cornerRadius = butttonFrame.size.height/2;
        [self.serialButton setTitle:self.post.serial.title forState:UIControlStateNormal];
        butttonFrame.origin.x += butttonFrame.size.width + VIEW_MARGIN;
    }
    if ( self.post.addr) {
        self.addrButton.hidden = NO;
        CGSize textSize = [self.post.addr sizeWithAttributes:@{NSFontAttributeName: self.addrButton.titleLabel.font}];
        butttonFrame.size = CGSizeMake(textSize.width+20, BUTTON_HEIGHT);
        self.addrButton.frame = butttonFrame;
        self.addrButton.layer.cornerRadius = butttonFrame.size.height/2;
        [self.addrButton setTitle:self.post.addr forState:UIControlStateNormal];
    }
}
- (CGFloat)heightWithPost:(Post*)post width:(CGFloat)width{
    return BUTTON_HEIGHT;
}
@end
